package com.evan.uyclub.OtherImageTool;

import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.text.format.Time;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.evan.uyclub.Other.Utlis;
import com.evan.uyclub.R;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sdsmdg.tastytoast.TastyToast;
import com.tapadoo.alerter.Alerter;
import com.xw.repo.BubbleSeekBar;

import java.io.FileNotFoundException;

public class MaoBoImg extends AppCompatActivity {

    Bitmap bitmap, bitmaps;
    private Button select_but, keep_but;
    private ImageView imgs, back, bol_image, rol_image;
    private BubbleSeekBar seekBar;
    private int imgint;

    @Override
    protected void onCreate(Bundle s) {
        super.onCreate(s);
        setContentView(R.layout.activity_mohuimg);

        //Find
        select_but = findViewById(R.id.select_but);
        keep_but = findViewById(R.id.keep_but);
        imgs = findViewById(R.id.compress_img);
        seekBar = findViewById(R.id.seekbar);
        bol_image = findViewById(R.id.blured_img);
        rol_image = findViewById(R.id.origin_img);

        //Run
        select_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Select();
            }
        });

        back = findViewById(R.id.back);
        back.setOnClickListener(view -> {
            this.finish();
        });

        //Smart回弹
        SmartRefreshLayout smart = (SmartRefreshLayout) findViewById(R.id.smarts);
        smart.setEnableLoadMore(true);
        smart.setEnablePureScrollMode(true);

        //run Sek
        Seek();

    }

    //打开相册并返回图片url
    public void Select() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, 1);
    }

    //protected是自动运行的，这里是通过返回来的url显示在img上
    @Override
    protected void onActivityResult(int result, int request, Intent data) {
        if (request == RESULT_OK) {
            Uri uri = data.getData();
            ContentResolver contentResolver = this.getContentResolver();
            try {
                bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri));
                bitmaps = Utlis.blur(MaoBoImg.this, bitmap);
                bol_image.setImageBitmap(bitmaps);
                rol_image.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        super.onActivityResult(result, request, data);

        keep_but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Time time = new Time();
                time.setToNow();
                int bith = time.month + time.monthDay;
                Utlis.fileSaveToPublic(MaoBoImg.this, "img" + bith, bitmaps);
                TastyToast.makeText(MaoBoImg.this, "模糊成功喽~", TastyToast.LENGTH_SHORT, TastyToast.SUCCESS).show();
                Alerter.create(MaoBoImg.this)
                        .setTitle(getString(R.string.keep_succes))
                        .setText(getString(R.string.keep_foto))
                        .setDuration(1200)
                        .setBackgroundColorRes(R.color.green)
                        .show();
            }

        });
    }

    public void Seek() {
        //监控方法
        seekBar.setOnProgressChangedListener(new BubbleSeekBar.OnProgressChangedListener() {
            @Override
            //时时刻刻值
            public void onProgressChanged(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat, boolean fromUser) {
                imgint = progress;
                rol_image.setAlpha((int) (255 - imgint * 2.55));
            }

            @Override
            //滑动停止时
            public void getProgressOnActionUp(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat) {

            }

            @Override
            public void getProgressOnFinally(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat, boolean fromUser) {
            }
        });
    }

}


/**
 * 我从网上找到了两种方法可实现
 * <p>
 * 首先说一下图片选择代码如下:
 * <p>
 * <p>
 * *打开本地相册选择图片
 * private void selectPic(){
 * //intent能够应用于广播和发起意图，其中属性有：ComponentName,action,data等
 * Intent intent=new Intent();
 * intent.setType("image/*");
 * //action表示intent的类型，能够是查看、删除、发布或其余状况；咱们选择ACTION_GET_CONTENT，系统能够根据Type类型来调用系统程序选择Type
 * //类型的内容给你选择
 * intent.setAction(Intent.ACTION_GET_CONTENT);
 * //若是第二个参数大于或等于0，那么当用户操做完成后会返回到本程序的onActivityResult方法
 * startActivityForResult(intent,1);
 * }
 * <p>
 * *把用户选择的图片显示在imageview中
 *
 * @Override prected void onActivityResult(int requestCode, intresultCode, Intnt data) {
 * /        操做完成，结果码返回是-1，即RESULT_OK
 * if(resultCode==RESULT_OK){
 * //获取选中文件的定位符
 * Uri uri =data.getData();
 * Log.e("uri",uri.toString());
 * //使用content的接口
 * ContentResolver cr =this.getContentResolver();
 * try {
 * //获取图片
 * Bitmap bitmap =BitmapFactory.decodeStream(cr.openInputStream(uri));
 * image.setImageBitmap(bitmap);
 * }catch (FileNotFoundException e){
 * Log.e("Exception",e.getMessage(),e);
 * }
 * }else{
 * //操做错误或没有选择图片
 * Log.i("MainActivtiy","operation error");
 * }
 * super.onActivityResult(requestCode,resultCode,data);
 * }
 * <p>
 * <p>
 * <p>
 * <p>
 * 1——方法  <用Glide框架提供的方法来容易实现>
 * <p>
 * --导入库
 * implementation 'com.github.bumptech.glide:glide:3.7.0'
 * implementation 'jp.wasabeef:glide-transformations:3.0.1'
 * <p>
 * --创建Glide并返回显示
 * Glide.with(this) .load(bitmapToUri(bitmap,this))
 * //设置高斯模糊 “23”：设置模糊度(在0.0到25.0之间)，默认”25";"4":图片缩放比例,默认“1”。
 * .apply(bitmapTransform(new BlurTransformation(25, 4)))
 * .into(imageVideoBackImage);
 * <p>
 * --调用函数是这个：
 * public static Uri bitmapToUri(Bitmap bitmap, Context context) {
 * Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, null, null));
 * return uri;
 * }
 * <p>
 * </用Glide框架提供的方法来容易实现>
 * <p>
 * <p>
 * 第二种方法：
 * <p>
 * 访问：https://www.jianshu.com/p/46ef85bcb433
 * <p>
 * 这个是动态实现方法;
 */

